package com.javabasic.concurrency;

import java.util.Random;
import java.util.concurrent.*;

public class TestScheduledThreadPool {

    public static void main(String[] args) {

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 5; i++) { // 生成5个任务 给线程池调度
            Future<Integer> future = scheduledExecutorService.schedule(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int num = new Random().nextInt(); // 生成随机数

                    System.out.println(Thread.currentThread().getName()+" " + num);

                    return num;
                }
            },3, TimeUnit.SECONDS);//延迟3后执行该线程任务

            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }


        scheduledExecutorService.shutdown();


    }
}
